Hello and welcome.
In this video we are going to create javascript functionality for our game.
The first thing I want to do is create the javascript file I've already given it a reference name here
inside his team Mail Online 33 have called it my script Duchess.
So I'm going to create it by clicking on the new file option clicking on file.
And then you save this new file to save as hallum going to save it as my script.
Yes.
So my script.
Yes.
So it's called my script which is going to my index.
You can see the name of the same script.
Yes.
It's important that the name you linked it to in your email file.
How can you make sure it's the name of the outro farm if not it will not work.
The first thing I want to do is add some constant variables.
These are the variable a constant physically variable that should not change in value.
Okay so we've created a constancy code halls and set it to court documents or query select all and pass
to the class.
Doctor here followed by the name is a class so passed to the class hall.
So these classes have already been defined inside his team.
Also I go into the hastier mail you can see we've got a class here or two score of the class called
Hall.
And the class called more.
So these are the classes I'm referring to inside this javascript document dot query select all or both
physically same method this query select or is a method that returns all the elements in the document
that matches is specified synthesis select or select.
The next thing I want to do is create a function that all create a random time for the all to pop out
of the hall.
So I'm just going to.
This is a function of defined here.
I've called it a function called random time random time so it would generate random time and have passed
it.
And Max.
These are functions on their on methot So this will return the maths stop round maths round beasley's
use to round up their numbers to the nearest term whole integer number.
And this is a maths random.
This will randomly generate numbers and will use the mouse thought round to round up the numbers to
the nearest whole integer.
So we're multiplying the random number generator.
And by the max maximum number minus the minimum number plus the minimum number.
So these are all different functions of methods.
So once the random numbers generated were play the max minus mean Plus mean someone to say that so I
can see how this works are going to run this inside the console.
So this is a project and will not open up the index or to email and click on 12 on the keyboard to open
up the console.
So now that I've got the console open I'm going to run this function called round.
Time inside here and then pass it in minimum and maximum time.
So here I am just going to type it inside the console typing random
time and then decided this is the minimum value would be 20 milliseconds and the maximum will be 2 seconds.
All right so now if I press pentair he should randomly generate a time taken see it.
Thus the one random time I do again and again and again you can see.
So we know we've got that functionality working next and I want to do is create on another function.
Is going to be a random function that will determine the random Hall this at all.
Wells So to determine the random halls from which the mall will pick his head or pop out from I have
added the function that all randomly specify the holes from where the moles was going to pop up from.
So it's called the function random hall and I've passed in the halls.
Okay so this was actually the holes and then line 10 here have got a variable chord index.
It's a constant that means when unexpected to change I have set that course to the max top floor and
then we've got the maths at random as well so it would generate a similar process to this random time
masten Random we generate the random time and then we've got the master top floor.
Well be run that opted in.
So the last thought flow function will return the largest integer less than or equal to a given number
and the mouse or random generates the random number and then we multiply that by the holes left.
So we know we have six altes so that's represented by the lente and here again.
A constant variable called hall setting to cause today calls and then inside the square brackets are
passing in this variable index So the index basically refers to the array of holds the first index would
be zero and then 1 2 3 4 5 and so on.
Okay so that's the index so this line 13 here is just so constant log to that I can test it to make
sure that it is randomly assigning the holes to all the holes that the mall is going to pop up from
someone and say this and open up the document and will go inside the console and quickly check this
out so I open up the hasty email file and also clicked on F 12 for my cable to open up this console.
So here all I need to do is type in the name of the function which is run on whole.
And I am one of past holes.
Had just press pent up an issue you can see is generated around who's generated random all.
Number two try again.
Give me all number one this time Hole number for this time on number 5.
Excellent So this is what I wanted to add for the holes to be randomly assigned where the mall is going
to pop out from one of the things that may occur or when you generate in random numbers is that you
may get the same number generated twice or more.
So what we want to do we don't want we want to avoid that so that we can have different calls assigned
different numbers.
So one way to do I need to put a conditional statement in that will prevent this same all from getting
the same number.
Okay all right so did the conditional statement which is this block here.
So basically I am saying what I need to do is define key variable or peer.
So I go online for here.
I've just added a statement here lets statement and give it the value of last hole.
The little statement basically declares a block scope local variable so that each option only has to
initialize for the value to be active so it allows you to declare variables that are limited in scope
to the block statement or the expression on which it is used in a way it's different from the variable
keyword which defines variable globally or locally to an entire function regardless of the block scope.
So now that we've defined the little statement here on on line for now implemented it here.
So basically I'm saying if the hall is strictly equal to the last hole.
Okay that if the numbers are the same we want to log this message on the console.
That's the same number and what it will do it.
We don't really render this function called random hole and regenerate in New numbers so the objective
of this we don't want the same halls to have the same number.
To warn them to be around omni generators or where there's the same number.
Got this written return here.
Reach render this function it calls it function again to render a mother random number for that call
where the last no had for example we've had the last number generator for a horse too.
And you generate two again instead of randomly assigned him that is to really render this function to
create a new random number.
And here we are calling this here this left statement and we set it up to YOU course to the whole day
and we are now returning all that has been assigned randomly.
So that is it for this part of the ledger on one side of this and then part two.
We will try and complete the rest of the script for this project.
Thank you for watching.
And bye for now.